package com.yxx.data.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yxx.data.domain.LabeledWound;
import com.yxx.data.domain.Wound;
import com.yxx.data.service.LabeledWoundService;
import com.yxx.data.service.WoundService;
import com.yxx.data.vo.LabeledWoundVo;
import com.yxx.sys.common.AppFileUtils;
import com.yxx.sys.common.AppSecureUtils;
import com.yxx.sys.common.DataGridView;
import com.yxx.sys.common.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yxx
 * @since 2022-08-09
 */
@RestController
@RequestMapping("/labeled-wound")
@Api("创面标注图片控制器")
public class LabeledWoundController {

  @Autowired
  LabeledWoundService labeledWoundService;

  @Autowired
  WoundService woundService;

  @GetMapping("/loadAllInformation")
  @ApiOperation("查询创面标注图片")
  public DataGridView loadAllInformation(LabeledWoundVo labeledWoundVo) {
    IPage<LabeledWound> page = new Page<LabeledWound>(labeledWoundVo.getPage(), labeledWoundVo.getLimit());
    QueryWrapper<LabeledWound> queryWrapper = new QueryWrapper<LabeledWound>();
    queryWrapper.eq(labeledWoundVo.getDepth()!=null && labeledWoundVo.getDepth()!=-1, "depth", labeledWoundVo.getDepth());
    queryWrapper.orderByDesc("id");
    labeledWoundService.page(page, queryWrapper);
    return new DataGridView(page.getTotal(), page.getRecords());
  }

  @PostMapping("/addInformation")
  @ApiOperation("添加创面标注图片")
  public Result addInformation(LabeledWoundVo labeledWoundVo) {

    //根据照片编号 获得照片存储相对地址和深度
    Wound wound = woundService.getOne(new QueryWrapper<Wound>().eq("picture_id", labeledWoundVo.getPictureId()));
    labeledWoundVo.setPicturePath(wound.getPicturePath());
//    labeledWoundVo.setDepth(wound.getDepth());
    labeledWoundVo.setLabelPath(AppFileUtils.DEFAULT_IMG_RELA_PATH);
    labeledWoundVo.setCreatedTime(LocalDate.now());
    labeledWoundVo.setChangeTime(LocalDate.now());
    boolean succ = labeledWoundService.save(labeledWoundVo);
    if(!succ) return Result.ADD_REEOR;
    return Result.ADD_SUCCESS;
  }

  @PostMapping("/updateInformation")
  @ApiOperation("更新创面标注图片")
  public Result updateInformation(LabeledWoundVo labeledWoundVo) {
    String labelPath = AppFileUtils.renameFile(labeledWoundVo.getLabelPath(), labeledWoundVo.getPictureId(),AppFileUtils.UPLOAD_CM_LABEL_PATH);
    labeledWoundVo.setLabelPath(labelPath);
    try {
      boolean b = labeledWoundService.updateById(labeledWoundVo);
      if(!b) return Result.UPDATE_REEOR;
      return Result.UPDATE_SUCCESS;
    } catch (Exception e) {
      e.printStackTrace();
      return Result.UPDATE_REEOR;
    }
  }

  @PostMapping("/deleteInformation")
  @ApiOperation("删除创面标注图片")
  public Result deleteInformation(Integer id) {
    try {
      LabeledWound labeledWound = labeledWoundService.getOne(new QueryWrapper<LabeledWound>().eq("id", id));
      boolean b = AppFileUtils.removeFile(new File(AppFileUtils.UPLOAD_CM_LABEL_PATH + labeledWound.getLabelPath()));
      if(!b) return Result.DELETE_REEOR;
      labeledWoundService.removeById(id);
      return Result.DELETE_SUCCESS;
    } catch (Exception e) {
      e.printStackTrace();
      return Result.DELETE_REEOR;
    }
  }

}
